package iv1201project.view;

import iv1201project.controller.RecruitDAO;
import iv1201project.extras.Helpers;
import iv1201project.model.*;
import java.io.*;
import java.io.UnsupportedEncodingException;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletResponse;

/**
 * The class responsible for recieving admin-session information, connected to the
 * recruiter login process.
 */
@Named("recruitmanager")
@SessionScoped
public class RecruitManager implements Serializable {

    @EJB
    private RecruitDAO recruitDAO;
    @Inject
    private RecruitDTO recruitDTO;
    private String appList = "";
    private int pdfID;

    /**
     * Add a new recruiter.
     */
    @Logthis
    public void addRecruiter() {
        recruitDAO.addRecruiter(getRecruitDTO());
        recruitDTO.setPassword("");
        recruitDTO.setUsername("");
    }

    /**
     * Upload a PDF.
     * 
     * @return A string representing a navigation rule.
     * 
     * @throws FileNotFoundException If the file is not found.
     * @throws IOException
     */
    @Logthis
    public String uploadPDF() throws FileNotFoundException, IOException {
        try {
            String tmp = recruitDAO.getPersonalInfo(pdfID);
            String filename = new Helpers().toMD5(tmp);
            new Helpers().createPDF("C:/" + filename + ".pdf", tmp);

            FacesContext context = FacesContext.getCurrentInstance();
            HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();

            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

            int read = 0;
            byte[] bytes = new byte[1024];
            OutputStream os = null;
            ByteArrayInputStream bis1;

            RandomAccessFile f = new RandomAccessFile("C:/" + filename + ".pdf", "r");
            byte[] b = new byte[(int)f.length()];
            f.read(b);

            try {
                bis1 = new ByteArrayInputStream(b);
                os = response.getOutputStream();

                while ((read = bis1.read(bytes)) != -1) {
                    os.write(bytes, 0, read);
                }
                os.flush();
                os.close();
            } catch (UnsupportedEncodingException e) {
                return "error";
            } catch (IOException e) {
                return "error";
            }

            FacesContext.getCurrentInstance().responseComplete();
        } catch (Exception ex) {
            return "error";
        }
        
        return "case1";
    }

    /**
     * @return the appList
     */
    public String getAppList() {
        return appList;
    }

    /**
     * @param appList the appList to set
     */
    public void setAppList(String appList) {
        this.appList = appList;
    }

    /**
     * @return the recruitDTO
     */
    public RecruitDTO getRecruitDTO() {
        return recruitDTO;
    }
    
    public void getList() {
        appList = recruitDAO.listApplicants();
    }

    /**
     * @param recruitDTO the recruitDTO to set
     */
    public void setRecruitDTO(RecruitDTO recruitDTO) {
        this.recruitDTO = recruitDTO;
    }

    /**
     * @return the pdfID
     */
    public int getPdfID() {
        return pdfID;
    }

    /**
     * @param pdfID the pdfID to set
     */
    public void setPdfID(int pdfID) {
        this.pdfID = pdfID;
    }
}
